package com.hl.qzzpbackend.service.impl;

import com.hl.qzzpbackend.config.EmailConfig;
import com.hl.qzzpbackend.model.enums.SendEmailCodeEnum;
import com.hl.qzzpbackend.service.AsyncService;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 异步服务
 *
 * @author helong
 */
@Service
public class AsyncServiceImpl implements AsyncService {

    @Resource
    private EmailConfig emailConfig;
    @Resource
    private JavaMailSender javaMailSender;

    /**
     * 发送邮箱验证码
     * @param email 邮箱
     * @param code 验证码
     * @param sendEmailCodeEnum 发送邮箱验证码类型
     */
    @Async
    @Override
    public void sendEmailCode(String email, String code, SendEmailCodeEnum sendEmailCodeEnum) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(emailConfig.getEmailFrom());
        message.setTo(email);
        message.setSubject(sendEmailCodeEnum.getTitle());
        String content = "验证码为 " + code + ",有效期为10分钟";
        message.setText(content);
        javaMailSender.send(message);
    }
}
